package com.eam.eventhandler;

import com.eam.data.EamEquipmentShutdown;
import com.eam.data.EamShutdownItem;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;

public class EmesShutdownItemEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamShutdownItem.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		
		String tabId = SessionInfo.getProcessId();
		
		if(tabId == null){
			return ;
		}
		//获取计划
				EamShutdownItem item = (EamShutdownItem) event.getTargetInstance();
				
				// 将要更改的实体类
				final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamShutdownItem.ENTITY_NAME);
				final Property typeProperty = detailsEntity.getProperty(EamShutdownItem.PROPERTY_DUTYDATE);
				
				String plan_Id = item.getEamEquipmentShutdown().getId(); //获取所属计划的ID
				
				String plannumber = item.getDutyDate();
				
				Long numbers = Long.parseLong(plannumber); //获取新增记录的划分时间
				//获取该条记录的停机总时长
				String aql = "select * from Emes_equipment_shutdown where Emes_equipment_shutdown_id = ? ";
				
				EamEquipmentShutdown query = (EamEquipmentShutdown)OBDal.getInstance().getSession().createSQLQuery(aql).addEntity(EamEquipmentShutdown.ENTITY_NAME).setString(0, plan_Id).uniqueResult();
				
				Long countTiME = query.getRunTime();
				
				//获取该条记录已划分时间
				
				String wql = "select sum(dutydate) from Eam_Shutdown_Item where Emes_equipment_shutdown_id = ? ";
				
				Long  countItem  = (Long)OBDal.getInstance().getSession().createSQLQuery(wql).setString(0, plan_Id).uniqueResult();
				
				if(countItem == null){
					countItem  =  0L;
				}
				//Long counItem = Long.parseLong(countItems);
				
				Long ItemTime = countItem + numbers.longValue();
				
				if(countTiME >= ItemTime ){
					event.setCurrentState(typeProperty, plannumber);
				}else{
					String language = OBContext.getOBContext().getLanguage().getLanguage();
			        ConnectionProvider conn = new DalConnectionProvider(false);
					throw new OBException(Utility.messageBD(conn, "责任时间已超过停机总时长，请重新划分！", language));
				}
		
	}
	
	
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		
		//获取计划
		EamShutdownItem item = (EamShutdownItem) event.getTargetInstance();
		
		String result = "Y";
		
		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamShutdownItem.ENTITY_NAME);
		final Property typeProperty = detailsEntity.getProperty(EamShutdownItem.PROPERTY_DUTYDATE);
		
		String plan_Id = item.getEamEquipmentShutdown().getId(); //获取所属计划的ID
		
		String plannumber = item.getDutyDate();
		
		Long numbers = Long.parseLong(plannumber); //获取新增记录的划分时间
		//获取该条记录的停机总时长
		String aql = "select * from Emes_equipment_shutdown where Emes_equipment_shutdown_id = ? ";
		
		EamEquipmentShutdown query = (EamEquipmentShutdown)OBDal.getInstance().getSession().createSQLQuery(aql).addEntity(EamEquipmentShutdown.ENTITY_NAME).setString(0, plan_Id).uniqueResult();
		
		Long countTiME = query.getRunTime();
		
		//获取该条记录已划分时间
		
		String wql = "select sum(dutydate) from Eam_Shutdown_Item where Emes_equipment_shutdown_id = ? ";
		
		Long  countItem  = (Long)OBDal.getInstance().getSession().createSQLQuery(wql).setString(0, plan_Id).uniqueResult();
		
		if(countItem == null){
			countItem  =  0L;
		}
		//Long counItem = Long.parseLong(countItems);
		
		Long ItemTime = countItem + numbers.longValue();
		
		if(countTiME >= ItemTime ){
			event.setCurrentState(typeProperty, plannumber);
		}else{
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, "责任时间已超过停机总时长，请重新划分！", language));
		}
		
	}
	
	public void onDelete(@Observes
	EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
	
}
